﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using elibrary.Classes;
using elibrary.Models.Managers;

namespace elibrary.Models.Entities
{
    public class Category
    {
        /* Class Fields [Start] ----------------------------------------------------------------- */
        [Key]
        public int id { get; set; }
        public string name { get; set; }
        /* Class Fields [End] ------------------------------------------------------------------- */

        /* Computed Fields [Start] -------------------------------------------------------------- */
        public string properName
        {
            get { return Utilities.toTitleCase(this.name); }
        }

        public Dictionary<string,object> marketplaceObject
        {
            get { return this.getMarketplaceObject(); }
        }
        /* Computed Fields [End] ---------------------------------------------------------------- */

        public void safelyDeleteMe()
        {
            BookManager bookManager = new BookManager(AppGlobals.dataContext);
            BookCategoryManager bcManager = new BookCategoryManager(AppGlobals.dataContext);
            CategoryManager categoryManager = new CategoryManager(AppGlobals.dataContext);

            List<Book> associatedBooks = bookManager.getAllByCategory(null,this.id);
            int uncatId = Category.ensureUncategorized().id;

            foreach(var book in associatedBooks)
            {
                if (book.bookCategories.Count <= 1)
                {
                    BookCategory bc = new BookCategory { bookId = book.id, categoryId = uncatId };
                    bcManager.add(bc,false);
                }

                BookCategory delete = bcManager.getByBookCategory(book.id,null,this.id);
                bcManager.delete(delete,false);
            }

            categoryManager.delete(this);
        }

        /* NOTE: This method will cause database commit if uncategorized does not exists */
        public static Category ensureUncategorized()
        {
            CategoryManager categoryManager = new CategoryManager(AppGlobals.dataContext);
            Category uncategorized = null;

            if ((uncategorized = categoryManager.get("uncategorized")) == null)
                uncategorized = categoryManager.get("uncategorised");

            if (uncategorized == null)
            {
                uncategorized = new Category { name = "uncategorized" };
                categoryManager.add(uncategorized);
            }

            return uncategorized;
        }

        private Dictionary<string,object> getMarketplaceObject()
        {
            Dictionary<string,object> obj = Utilities.getClientRichObject(new string[]{"id"}, 
                this);

            obj["key"] = this.name;
            obj["desc"] = this.properName;

            return obj;
        }
    }
}